package com.museum.guide.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.museum.guide.entity.MuseumMap;
import com.museum.guide.mapper.MuseumMapMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MuseumMapService extends ServiceImpl<MuseumMapMapper, MuseumMap> {

    /**
     * 分页查询地图信息
     */
    public Page<MuseumMap> page(Page<MuseumMap> page, String name, String floor, Integer status) {
        LambdaQueryWrapper<MuseumMap> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(name), MuseumMap::getName, name)
                .eq(StringUtils.hasText(floor), MuseumMap::getFloor, floor)
                .eq(status != null, MuseumMap::getStatus, status)
                .orderByAsc(MuseumMap::getFloor);
        return page(page, wrapper);
    }

    /**
     * 获取所有启用的地图列表
     */
    public List<MuseumMap> listEnabled() {
        LambdaQueryWrapper<MuseumMap> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MuseumMap::getStatus, 1)
                .orderByAsc(MuseumMap::getFloor);
        return list(wrapper);
    }

    /**
     * 根据楼层获取地图
     */
    public List<MuseumMap> listByFloor(String floor) {
        LambdaQueryWrapper<MuseumMap> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MuseumMap::getFloor, floor)
                .eq(MuseumMap::getStatus, 1);
        return list(wrapper);
    }

    /**
     * 新增地图
     */
    public void saveMap(MuseumMap museumMap) {
        museumMap.setCreateTime(LocalDateTime.now());
        museumMap.setUpdateTime(LocalDateTime.now());
        save(museumMap);
    }

    /**
     * 更新地图信息
     */
    public void updateMap(MuseumMap museumMap) {
        museumMap.setUpdateTime(LocalDateTime.now());
        updateById(museumMap);
    }

    /**
     * 删除地图
     */
    public void removeMap(Long id) {
        removeById(id);
    }

    /**
     * 启用/禁用地图
     */
    public void updateStatus(Long id, Integer status) {
        MuseumMap map = getById(id);
        if (map != null) {
            map.setStatus(status);
            map.setUpdateTime(LocalDateTime.now());
            updateById(map);
        }
    }
}